const db = require('../db/index')
const getCurrentTime = require('../utile/getCurrentTime')
//添加文章
exports.addArticle = (req,res)=>{
    const content = req.body;
    console.log(content);
    const sql = 'insert into production set ?'
    db.query(sql,Object.assign(content,{timer:getCurrentTime.getNowDate()}), function (err, results) {
        // 执行 SQL 语句失败
        if (err) return res.send({ status: 0, message: err.message })
        // SQL 语句执行成功，但影响行数不为 1
        // 注册成功
        res.send({ status: 1, message: '添加文章成功' })
      })
}
// 获取所有文章
exports.getArticle = (req,res)=>{
  const sql = `select * from production`
  db.query(sql, function (err, results) {
    if (err) return res.send({ status: 0, message: err.message })

    res.send({msg:'获取成功',data:results})
  })
}

// 查找文章
exports.searchArticle = (req,res)=>{
  let body = req.body
  const sql = `select * from production where userId = ?`
  db.query(sql, body.userId,function (err, results) {
    if (err) return res.send({ status: 0, message: err.message })

    res.send({msg:'获取成功',data:results})
  })
}

// 评论文章
exports.comment=(req,res)=>{
  let body = req.body
  console.log(body);
  let sql = `UPDATE production SET PL = ? WHERE id = ?`
  db.query(sql,[body.PL,body.id],(err,result)=>{
    if (err) return res.send({ status: 0, message: err.message })

    res.send({msg:'评论成功'})
  })
}

// 点赞文章
exports.good=(req,res)=>{
  let body = req.body
  console.log(body);
  let sql = `UPDATE production SET DZ = ? WHERE id = ?`
  db.query(sql,[body.PL,body.id],(err,result)=>{
    if (err) return res.send({ status: 0, message: err.message })

    res.send({msg:'点赞成功'})
  })
}
